export function addNodeInfo(nodeType:any, beforeNode:any, currentNode:any, globalJson:any) {
    if (nodeType === "YwuExecutorNode") {
        beforeNode.next = {
            id: "nid_" + generateUUID(),
            type: "YwuExecutorNode",
            name: "执行人",
            properties: {name: "执行人", code: "nid_" + generateUUID(), peopleType: "CUSTOM", peopleInnerParam: [], execType: "OR"},
            next: currentNode
        };
    } else if (nodeType === "YwuApprovalNode") {
        beforeNode.next = {
            id: "nid_" + generateUUID(),
            type: "YwuApprovalNode",
            name: "审批人",
            properties: {name: "审批人", code: "nid_" + generateUUID(), peopleType: "CUSTOM", peopleInnerParam: [], execType: "OR"},
            next: currentNode
        };
    } else if (nodeType === "YwuServiceNode") {
        beforeNode.next = {
            id: "nid_" + generateUUID(),
            type: "YwuServiceNode",
            name: "服务节点",
            properties: {name: "服务节点", code: "nid_" + generateUUID()},
            next: currentNode
        };
    } else if (nodeType === "YwuConditionalNode") {
        beforeNode.next = {
            id: "nid_" + generateUUID(),
            type: "YwuConditionalNode",
            name: "条件分支",
            properties: {name: "条件分支", code: "nid_" + generateUUID()},
            childrenNodes: [
                {
                    type: "common",
                    conditionName: "条件1",
                    condition: {}
                },
                {
                    type: "other",
                    conditionName: "其他条件",
                    condition: {}
                }
            ],
            next: currentNode
        };
    } else if (nodeType === "YwuParallelNode") {
        beforeNode.next = {
            id: "nid_" + generateUUID(),
            type: "YwuParallelNode",
            name: "并行分支",
            properties: {name: "并行分支", code: "nid_" + generateUUID()},
            childrenNodes: [
                {
                    type: "common",
                    conditionName: "并行分支1",
                    next: {
                        id: "nid_" + generateUUID(),
                        type: "YwuExecutorNode",
                        name: "执行人",
                        properties: {name: "执行人", code: "nid_" + generateUUID()}
                    }
                },
                {
                    type: "common",
                    conditionName: "并行分支2",
                    next: {
                        id: "nid_" + generateUUID(),
                        type: "YwuApprovalNode",
                        name: "审批人",
                        properties: {name: "审批人", code: "nid_" + generateUUID()}
                    }
                }
            ],
            next: currentNode
        };
    }
    parseMultiInfo(globalJson);
    getMaxWidth(globalJson);
}

function randomNumber() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

function generateUUID() {
    return (randomNumber() + randomNumber() + "-" + randomNumber() + "-" + randomNumber() +
        "-" + randomNumber() + "-" + randomNumber() + randomNumber() + randomNumber());
}

export function getMaxWidth(jsonInfo:any) {
    let tempJson = jsonInfo;
    // 找到第一个分支节点，获取对应的数量
    while (true) {
        if (!tempJson) {
            jsonInfo.maxWidth = window.innerWidth;
            return;
        }
        if (tempJson.type === "YwuConditionalNode" || tempJson.type === "YwuParallelNode") {
            let windowWidth = window.innerWidth;
            let width = tempJson.width
            let maxWidth = width * 150 + (width - 1) * 100;
            if (windowWidth >= maxWidth) {
                jsonInfo.maxWidth = windowWidth;
            } else {
                jsonInfo.maxWidth = maxWidth;
            }
            return;
        }
        tempJson = tempJson.next;
    }
}

export function parseMultiInfo(jsonInfo:any) {
    if (!(jsonInfo && (jsonInfo.type !== "YwuConditionalNode" || jsonInfo.type !== "YwuParallelNode"))) {
        if (jsonInfo) {
            parseMultiInfo(jsonInfo.next)
        }
        return;
    }
    // 初始化
    jsonInfo.width = 0;
    // 递归遍历每一个分支
    if (!(jsonInfo.childrenNodes && jsonInfo.childrenNodes.length !== 0)) {
        parseMultiInfo(jsonInfo.next)
        return;
    }
    for (let index = 0; index < jsonInfo.childrenNodes.length; index++) {
        let childrenInfo = jsonInfo.childrenNodes[index];
        parseChildrenInfo(childrenInfo, jsonInfo);
    }
    parseMultiInfo(jsonInfo.next)
}

export function parseChildrenInfo(childrenInfo:any, jsonInfo:any) {
    if (jsonInfo.width) {
        jsonInfo.width = jsonInfo.width + 1;
    } else {
        jsonInfo.width = 1;
    }
    // 记录当前分支下的最大分支
    let maxWidth = 0;
    while (childrenInfo.next) {
        if (childrenInfo.next.type === "YwuConditionalNode" || childrenInfo.next.type === "YwuParallelNode") {
            childrenInfo.next.width = 0;
            for (let index = 0; index < childrenInfo.next.childrenNodes.length; index++) {
                let newChildInfo = childrenInfo.next.childrenNodes[index];
                parseChildrenInfo(newChildInfo, childrenInfo.next);
            }
            maxWidth = maxWidth > childrenInfo.next.width ? maxWidth : childrenInfo.next.width;
        }
        childrenInfo = childrenInfo.next;
    }
    jsonInfo.width += maxWidth;
}